package com.cg.nio;

import java.nio.ByteBuffer;

public class BufferTest {

    public static void main(String[] args) {
//        test01();
//        test02();
        test03();
    }

    private static void test03() {
        //创建直接内存缓冲区
        //直接内存效率(不占用堆内存) > 非直接内存(暂用堆内存)
        //做io处理时,直接内存会具有更高的效率，但是它比申请普通的堆内存需要耗费更高的性能
        //当由很大的数据要缓存，并且它的生命周期又很长,那么比较适合使用直接内存，一般来说，如果不能带来很明显的性能提升，还是推荐直接使用堆内存
        //应用场景：频繁的io操作或者很大的数据需要存储
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        System.out.println(byteBuffer.isDirect());  //判断缓存区属于直接内存还是非直接内存
    }

    private static void test02() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        String name = "cheyinbo";
        byteBuffer.put(name.getBytes());
        byteBuffer.flip();

        //读取数据
        byte[] bytes = new byte[2];
        byteBuffer.get(bytes);
        String s = new String(bytes);
        System.out.println(s);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        byteBuffer.mark();  //对于当前position位置进行标记
        byte[] bytes1 = new byte[3];
        byteBuffer.get(bytes1);
        String s1 = new String(bytes1);
        System.out.println(s1);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        //reset()返回标记位,下面的读取将由标记位重新开始进行读取
        byteBuffer.reset();
        if(byteBuffer.hasRemaining()){  //存在剩余
            System.out.println(byteBuffer.remaining()); //囧去剩余数
        }
        byte[] bytes2 = new byte[4];
        byteBuffer.get(bytes2);
        String s2 = new String(bytes2);
        System.out.println(s2);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");
    }

    private static void test01() {
        //分配一个缓冲区、容量设置为10
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        //put往缓冲区中添加数据
        String name = "cheyinbo";
        byteBuffer.put(name.getBytes());
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        //Buffer flip()为将缓冲区的界限设置为当前位置，并将当前位置设值为0，切换为可读模式
        byteBuffer.flip();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        //读取缓冲区数据，get()默认读取index为0的数据
        char c1 = (char)byteBuffer.get();
        System.out.println(c1);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");

        //clear清除缓冲区
        byteBuffer.clear();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //读取缓冲区数据，clear后并没有清除缓冲区数据，只是将指针指向了0
        char c2 = (char)byteBuffer.get();
        System.out.println(c2);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("--------------------------------");
    }

}
